//
//  Switch.m
//  GiftSay
//
//  Created by QQn1993 on 16/1/14.
//  Copyright © 2016年 liyinkai. All rights reserved.
//

#import "Switch.h"

@interface Switch ()

@property (nonatomic,strong) UIButton *btn;

@end

@implementation Switch

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.masksToBounds = YES;
        self.layer.borderWidth = 2;
        self.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.layer.cornerRadius = self.height / 2;
        self.backgroundColor = [UIColor greenColor];
        self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.btn.frame = CGRectMake(self.width - frame.size.height, 0, frame.size.height, frame.size.height);
        self.btn.layer.masksToBounds = YES;
        self.btn.layer.cornerRadius = self.height / 2;
        self.btn.layer.borderWidth = 2;
        self.btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.btn.backgroundColor = [UIColor whiteColor];
        [self.btn addTarget:self action:@selector(touch) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.btn];
    }
    return self;
}


- (void)touch
{
    self.btn.selected = !self.btn.selected;
    if (self.btn.selected == YES) {
        [UIView animateWithDuration:0.5 animations:^{
        self.btn.frame = CGRectMake(0, 0, self.height, self.height);
            
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"NightMode"];
            
        self.backgroundColor = [UIColor whiteColor];
        }];
    }
    else{
        [UIView animateWithDuration:0.5 animations:^{
            self.btn.frame = CGRectMake(self.width - self.height, 0, self.height, self.height);
            
            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"NightMode"];
            
            self.backgroundColor = [UIColor greenColor];
        }];
    }
    
    [self.delegate sendSwich:self.btn.selected];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
